package com.xiaoxi.bms.common.handler;

import com.xiaoxi.bms.common.exception.BMSSecurityException;
import com.xiaoxi.bms.common.response.ApiResponse;
import com.xiaoxi.bms.common.util.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author xiaoxi
 * @version 1.0
 * @date 2022/5/17 11:09
 */
@Component
@Slf4j
public class MyAuthFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException authenticationException) {
        HttpUtils.renderString(response, ApiResponse.ofSecurityException((BMSSecurityException) authenticationException), HttpStatus.BAD_REQUEST);
    }
}
